package com.example.rotateimageview.rotateImage

import android.graphics.Camera
import android.util.Log
import android.view.animation.Animation
import android.view.animation.Transformation

/**
 * author  : Liushuai
 * time    : 2021/8/29 9:30
 * desc    :
 */
class RotateImageAnimation(val fromDegrees:Float,val toDegrees:Float) :Animation() {
    private var centerX=0f
    private var centerY=0f
    private lateinit var mCamera: Camera
    private var currentRotateDegrees=0f

    override fun initialize(width: Int, height: Int, parentWidth: Int, parentHeight: Int) {
        super.initialize(width, height, parentWidth, parentHeight)
        centerX= (width / 2).toFloat()
        centerY= (height / 2).toFloat()
        mCamera= Camera()
    }

    override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
        currentRotateDegrees=fromDegrees+(toDegrees-fromDegrees)*interpolatedTime
        t?.matrix?.let {
            mCamera.save()
            mCamera.rotateY(currentRotateDegrees)
            mCamera.getMatrix(it)
            mCamera.restore()
            it.preTranslate(-centerX,-centerY)
            it.postTranslate(centerX,centerY)
        }
        super.applyTransformation(interpolatedTime, t)
    }
}